//
//  LLCommitOrderViewController.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/10.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLCommitOrderViewController.h"
#import "LLCommitOrderAddressCell.h"
#import "LLCommitOrderBottomView.h"
#import "LLCommitOrderGoodsHeader.h"
#import "LLShoppingCartModel.h"
#import "LLCommitOrderGoodsCell.h"

@interface LLCommitOrderViewController ()<LLCommitOrderAddressCellDelegate>
@property (strong, nonatomic) LLCommitOrderBottomView *bottomView;
@property (copy, nonatomic) NSString *noteStr;
@property (assign, nonatomic) CGFloat noteStrH;
@end

@implementation LLCommitOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rightNavTitle = @"取消";
    [self initBottomView];
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.tintColor = UIColor.blackColor;
    self.navigationItem.rightBarButtonItem.tintColor = UIColor.blackColor;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:LLRegularFont(18)}];
    self.navigationController.navigationBar.barTintColor = UIColor.whiteColor;
//    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.tintColor = UIColor.whiteColor;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:LLRegularFont(18)}];
    self.navigationController.navigationBar.barTintColor = LLColor(218, 13, 13);
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"底"] forBarMetrics:UIBarMetricsDefault];
}

- (void)initBottomView {
    _bottomView = [[LLCommitOrderBottomView alloc] initWithFrame:CGRectMake(0, LLScreenHeight-self.navH-50, LLScreenWidth, 50)];
    _bottomView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:_bottomView];
//    MJWeakSelf
    [_bottomView setDidClickCommitBlock:^{
        
    }];
}

- (void)configTableView {
    self.tableViewStyle = UITableViewStyleGrouped;
    [super configTableView];
    self.tableView.backgroundColor = LLColorRGB(235);
    self.tableView.height = LLScreenHeight-self.navH-50;
    [self.tableView registerCellByClassName:@"LLCommitOrderAddressCell"];
    [self.tableView registerCellByClassName:@"LLCommitOrderGoodsCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.models.count + 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section > self.models.count) {
        return 2;
    } else {
        LLShoppingCartSectionModel *sectionModel = self.models[section-1];
        return sectionModel.shoppingCarts.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 60;
    } else if (indexPath.section > self.models.count) {
        if (indexPath.section == self.models.count + 1 && indexPath.row == 1) {
            return self.noteStr? self.noteStrH : 44;
        }
        return 44;
    }
    return 105;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0 || section > self.models.count) {
        return 0.0001;
    }
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = LLColorRGB(235);
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0 || section > self.models.count) {
        return UIView.new;
    }
    LLCommitOrderGoodsHeader *header = [[LLCommitOrderGoodsHeader alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, 40)];
    header.backgroundColor = UIColor.whiteColor;
    LLShoppingCartSectionModel *sectionModel = self.models[section-1];
    header.title = sectionModel.title;
    return header;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LLCommitOrderAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLCommitOrderAddressCell"];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section > self.models.count) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCell"];
        }
        cell.textLabel.textColor = LLColorRGB(100);
        cell.textLabel.font = LLRegularFont(15);
        cell.textLabel.text = @"发票";
        cell.detailTextLabel.text = @"80";
        if (indexPath.section == self.models.count+1 && indexPath.row == 0) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        return cell;
    }
    LLCommitOrderGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLCommitOrderGoodsCell"];
    LLShoppingCartSectionModel *sectionModel = self.models[indexPath.section-1];
    cell.model = sectionModel.shoppingCarts[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

@end
